package com.alibaba.travel.article.controller;

import com.alibaba.travel.article.dto.ArticleAddRequest;
import com.alibaba.travel.article.dto.ArticleResponse;
import com.alibaba.travel.article.dto.ArticleUpdateRequest;
import com.alibaba.travel.article.service.ArticleService;
import com.alibaba.travel.common.utils.PageUtils;
import com.alibaba.travel.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;





/**
 * 文章信息表
 *
 * @author wherezy
 * @email wherezy@tom.com
 * @date 2023-02-23 15:08:54
 */
@Api(tags = {"文章信息表ArticleController"})
@RestController
@RequestMapping("article/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @ApiOperation(value="获取所有公开的文章详细信息", notes="")
    @PostMapping("/allArticle")
    public R getAllPublicArticle(){
        List<ArticleResponse> details = articleService.getAllPublicArticle();
        return R.ok().put("list", details);
    }

    @ApiOperation(value="通过文章ids 获取文章详细信息", notes="文章ids")
    @PostMapping("/details")
    public R getDetails(Long[] ids){
        List<ArticleResponse> details = articleService.getDetails(ids);
        return R.ok().put("list", details);
    }

    /**
     * 数据库搜索
     */
    @ApiOperation(value="通过标签id分页获取文章信息", notes="k-v map形式封装分页参数 + 排序")
    @PostMapping("/searchByTags")
    public R searchByTag(@RequestBody Map<String, Object> params){
        PageUtils page = null;
        try {
            page = articleService.searchByTags(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok().put("page", page);
    }

    /**
     * 分页私人文章列表
     */
    @ApiOperation(value="分页私人文章列表", notes="k-v map形式封装分页参数 + 排序")
    @PostMapping("/privateList")
    public R privateArticle(@RequestBody Map<String, Object> params){
        PageUtils page = null;
        try {
            page = articleService.queryPrivateArticlePage(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @ApiOperation(value="分页获取信息", notes="k-v map形式封装分页参数")
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page;
        try {
            page = articleService.queryPage(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation(value="获取文章详细信息")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        ArticleResponse article = articleService.getDetailById(id);

        return R.ok().put("article", article);
    }

    /**
     * 保存
     */
    @ApiOperation(value="保存")
    @PostMapping("/save")
    public R save(@RequestBody ArticleAddRequest article){
        try {
            articleService.save(article);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value="修改")
    @PostMapping("/update")
    public R update(@RequestBody ArticleUpdateRequest article){
        try {
            articleService.update(article);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value="删除")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        try {
            articleService.delete(Arrays.asList(ids));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok();
    }

}
